package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.doman.Result;
import com.itheima.doman.pojo.Clazz;
import com.itheima.doman.pojo.ClazzQueryParam;
import com.itheima.doman.pojo.PageResult;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    /**
     *  添加班级
     * @param clazz
     * @return
     */
    @Log
    @PostMapping
    public Result addClazz(@RequestBody Clazz clazz){
        clazzService.addClazz(clazz);
        return Result.success();
    }

    /**
     *  查询所有班级
     * @return
     */

    @GetMapping("/list")
    public Result findAll(){
       List<Clazz> clazzList= clazzService.findAll();
       return Result.success(clazzList);
    }

    /**
     *  分页查询班级
     * @param clazzQueryParam
     * @return
     */
    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam){
        PageResult<Clazz> pageResult= clazzService.page(clazzQueryParam);
        log.info("查询班级信息, {}", clazzQueryParam);
        return Result.success(pageResult);
    }

    /**
     *  删除班级
     * @param id
     * @return
     */
    @Log
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        clazzService.deleteById(id);
        return Result.success();
    }

    /**
     *  根据id回显信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Clazz clazz= clazzService.findById(id);
        return Result.success(clazz);
    }

    /**
     *  修改班级
     * @param clazz
     * @return
     */
    @Log
    @PutMapping
    public Result updateClazz(@RequestBody Clazz clazz){
        clazzService.updateClazz(clazz);
        return Result.success();
    }
}
